/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.centerrecord.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.account.entity.Account;
import com.jeeplus.modules.account.mapper.AccountMapper;
import com.jeeplus.modules.account.service.AccountService;
import com.jeeplus.modules.member.entity.Member;
import com.jeeplus.modules.member.service.MemberService;
import com.jeeplus.modules.ordersstore.entity.OrdersStore;
import com.jeeplus.modules.rewardparameters.entity.RewardParameters;
import com.jeeplus.modules.rewardparameters.mapper.RewardParametersMapper;
import com.jeeplus.modules.rewardparameters.service.RewardParametersService;
import com.jeeplus.modules.subgoods.entity.SubGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.modules.centerrecord.entity.CenterRecord;
import com.jeeplus.modules.centerrecord.mapper.CenterRecordMapper;

/**
 * 服务中心收益记录Service
 * @author w
 * @version 2021-04-06
 */
@Service
@Transactional(readOnly = true)
public class CenterRecordService extends CrudService<CenterRecordMapper, CenterRecord> {
	@Autowired
	private MemberService memberService;
	@Autowired
	private AccountService accountService;
	@Autowired
	private RewardParametersService rewardParametersService;
	@Autowired
	private AccountMapper accountMapper;
	@Autowired
	private RewardParametersMapper rewardParametersMapper;

	public CenterRecord get(String id) {
		return super.get(id);
	}
	
	public List<CenterRecord> findList(CenterRecord centerRecord) {
		return super.findList(centerRecord);
	}
	
	public Page<CenterRecord> findPage(Page<CenterRecord> page, CenterRecord centerRecord) {
		return super.findPage(page, centerRecord);
	}
	
	@Transactional(readOnly = false)
	public void save(CenterRecord centerRecord) {
		super.save(centerRecord);
	}
	
	@Transactional(readOnly = false)
	public void delete(CenterRecord centerRecord) {
		super.delete(centerRecord);
	}

	/**
	 * 处理服务中心认购收益
	 * @param member
	 * @param subGoods
	 * @param money
	 */
	@Transactional(readOnly = false)
	public void manageCenterRecord(Member member, SubGoods subGoods, Double money) {
		//服务中心认购奖励比例
		Double percentService = 0.0;
		List<Member> effectiveList = new ArrayList<>();
		List<Member> centerMembers = memberService.findCenterMembers();
		if (centerMembers.size()>0){
//			for (Member centerMember : centerMembers) {
//				boolean  isEffective = memberService.isOrNotEffective(centerMember);
//				if (isEffective){
//					effectiveList.add(centerMember);
//				}
//			}
			//查询奖励比例
			List<RewardParameters> rewardParametersList = rewardParametersMapper.findList(new RewardParameters());
			if (rewardParametersList.size()>0){
				percentService = rewardParametersList.get(0).getSubPercentService();
			}
			//每个中心分到的钱
			Double sendMoney = BigDecimal.valueOf(money*percentService)
					.divide(BigDecimal.valueOf(centerMembers.size()),2,BigDecimal.ROUND_HALF_DOWN).doubleValue();
			for (Member centerMember : centerMembers) {
//				boolean  isEffective = memberService.isOrNotEffective(centerMember);
				//是相同地区的
//				if (centerMember.getRegionPlace().equals(subGoods.getPosition())){
					//2%收益  平均分
//				if (isEffective){
					Account account = accountService.get(centerMember.getAccount().getId());
					if (account != null){
						account.setYue(account.getYue()+sendMoney);
						account.setTodayProfit(account.getTodayProfit()+sendMoney);
						account.setTodayDynamic(account.getTodayDynamic()+sendMoney);
						account.setTotalProfit(account.getTotalProfit()+sendMoney);
						account.setDynamicProfit(account.getDynamicProfit()+sendMoney);
//						accountService.save(account);
						accountMapper.saveDynamic(sendMoney,account.getId());
						//记录
						CenterRecord centerRecord = new CenterRecord();
						centerRecord.setFromMem(member);
						centerRecord.setMember(centerMember);
						centerRecord.setGoodsName(subGoods.getGoodsName());
						centerRecord.setMoney(sendMoney);
						centerRecord.setCatchType("0");
						save(centerRecord);
					}
//				}
//				}
			}
		}
	}

	/**
	 * 处理服务中心积分兑换收益
	 * @param member
	 * @param ordersNo
	 * @param payIntegral
	 */
	@Transactional(readOnly = false)
	public void manageCenterIntegral(Member member, Double payIntegral, String ordersNo) {
		//服务中心手续费奖励比例
		Double percentService = 0.0;
		List<Member> effectiveList = new ArrayList<>();
		//查询奖励比例
		List<RewardParameters> rewardParametersList = rewardParametersService.findList(new RewardParameters());
		if (rewardParametersList.size()>0){
			percentService = rewardParametersList.get(0).getIntePercentService();
		}
		List<Member> centerMembers = memberService.findCenterMembers();
		if (centerMembers.size()>0) {
//			for (Member centerMember : centerMembers) {
//				boolean  isEffective = memberService.isOrNotEffective(centerMember);
//				if (isEffective){
//					effectiveList.add(centerMember);
//				}
//			}
			// 每个中心分到的钱
			Double sendMoney = BigDecimal.valueOf(payIntegral * percentService)
					.divide(BigDecimal.valueOf(centerMembers.size()), 2, BigDecimal.ROUND_HALF_DOWN).doubleValue();
			for (Member centerMember : centerMembers) {
//				boolean  isEffective = memberService.isOrNotEffective(centerMember);
//				if (isEffective){
					//1%收益
					Account account = accountService.get(centerMember.getAccount().getId());
					if (account != null) {
						account.setYue(account.getYue() + sendMoney);
						account.setTodayProfit(account.getTodayProfit() + sendMoney);
						account.setTodayDynamic(account.getTodayDynamic() + sendMoney);
						account.setTotalProfit(account.getTotalProfit() + sendMoney);
						account.setDynamicProfit(account.getDynamicProfit() + sendMoney);
//						accountService.save(account);
						accountMapper.saveDynamic(sendMoney,account.getId());
						//记录
						CenterRecord centerRecord = new CenterRecord();
						centerRecord.setFromMem(member);
						centerRecord.setMember(centerMember);
//						centerRecord.setGoodsName(order.ge);
						centerRecord.setOrderNo(ordersNo);
						centerRecord.setMoney(sendMoney);
						centerRecord.setCatchType("1");
						save(centerRecord);
					}
//				}
			}
		}
	}

    public List<CenterRecord> findListByDays(CenterRecord centerRecord) {
		return mapper.findListByDays(centerRecord);
    }
}